package cn.tedu.lucky.draw.server.job.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HelloJobConfig {

    /**
     * 作业启动器
     */
    @Autowired
    private JobLauncher jobLauncher;

    /**
     * Job 构建工厂
     * 用于构造Job对象
     */
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    /**
     * Step 构造工厂
     * 用于构造Step对象
     */
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Step step(){
        //tasklet()该方法用于指定step的逻辑，类似于Thread
        return stepBuilderFactory.get("step") //指定步骤名称
                .tasklet((contribution,chunkContext)-> {
                    System.out.println("Hello,Spring Batch!");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

    @Bean
    public Job job(){
        return jobBuilderFactory.get("helloJob")
                .start(step())
                .build();
    }
}
